home *** CD-ROM | disk | FTP | other *** search
/ The World's Largest Collection of Windows Software / The World's Largest Collection of Windows Software - Disc 1.iso / connect / _j2 / wvnsc926 / rcs / wvfiler.c < prev    next >
C/C++ Source or Header  |  1994-09-21  |  12KB  |  447 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     94.09.16.01.01.04;  author jcooper;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @Smart filer: extension mapping, name shortening, etc
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/* ------------------------------------------------------------------------
  27.  * File handlers
  28.  *
  29.  * author: john s. cooper
  30.  * sept 10, 1994
  31.  *
  32.  */
  33. #include <windows.h>
  34. #include <windowsx.h>
  35. #include "wvglob.h"
  36. #include "winvn.h"
  37. #pragma hdrstop
  38. #include <io.h>            // for _access
  39. #include <stdlib.h>        // itoa
  40.  
  41. #define MAXFILTERLEN 512
  42.  
  43. /* ------------------------------------------------------------------------
  44.  * File/path open routines
  45.  * jsc
  46.  */
  47. void    /* Set up filters */
  48. GenerateFileFilters (HWND hParentWnd, char *filters)
  49. {
  50.   register int i;
  51.   int len;
  52.   char chReplace;
  53.  
  54.   if ((len = LoadString (hInst, IDS_FILTERSTRING, filters, MAXFILTERLEN - 1)) == 0) {
  55.     MessageBox (hParentWnd, "No Filters Available", "String Load Failure", MB_OK);
  56.     *filters = '\0';
  57.   }
  58.   else {
  59.     chReplace = filters[len - 1];   /* grab separator */
  60.  
  61.     for (i = 0; filters[i] != '\0'; i++)
  62.       if (filters[i] == chReplace)
  63.    filters[i] = '\0';
  64.   }
  65. }
  66.  
  67. /* ------------------------------------------------------------------------
  68.  *  Asks for a path
  69.  *  jsc
  70.  */
  71. BOOL
  72. AskForFilePath (HWND hParentWnd, char *pathName, char *title)
  73. {
  74.     if (!DialogBoxParam (hInst, (LPCSTR) "WinVnSelectPath", hParentWnd, (DLGPROC) lpfnWinVnSelectPathDlg, (LPARAM)pathName))
  75.         return (FAIL);         
  76.     strcpy (pathName, strlwr(DialogString));
  77.  
  78.     return (SUCCESS);
  79. }
  80.  
  81. /* ------------------------------------------------------------------------
  82.  * Asks for a filename which must exist
  83.  */
  84. BOOL
  85. AskForExistingFileName (HWND hParentWnd, char *fileName, char *title)
  86. {
  87.   OPENFILENAME ofn;
  88.   char szFile[MAXFILENAME];
  89.   char filters[MAXFILTERLEN];
  90.  
  91.   GenerateFileFilters (hParentWnd, filters);
  92.  
  93.   memset (&ofn, 0, sizeof (OPENFILENAME));
  94.   szFile[0] = '\0';
  95.   ofn.lpstrTitle = title;
  96.   ofn.lStructSize = sizeof (OPENFILENAME);
  97.   ofn.hwndOwner = hParentWnd;
  98.   ofn.lpstrFilter = filters;
  99.   ofn.nFilterIndex = 1;
  100.   ofn.lpstrFile = szFile;
  101.   ofn.nMaxFile = sizeof (szFile);
  102.   ofn.lpstrFileTitle = NULL;
  103.   ofn.Flags = OFN_SHOWHELP | OFN_FILEMUSTEXIST;
  104.  
  105.   if (GetOpenFileName (&ofn) == 0)
  106.     return (FAIL);
  107.   strcpy (fileName, strlwr (szFile));
  108.   return (SUCCESS);
  109. }
  110. /* ------------------------------------------------------------------------
  111.  * Check if file name is OK based on SmartFiler settings.  If not, prompt user new name
  112.  * Check if file already exists.  If so, prompt user OK to overwrite
  113.  * If fileName contains a name on entry, check if it's OK, if yes-done
  114.  */
  115. BOOL
  116. AskForNewFileName (HWND hParentWnd, char *fileName, char *startDir, BOOL appendOk)
  117. {
  118.   OPENFILENAME ofn;
  119.   char mybuf[MAXINTERNALLINE];
  120.   char szDirName[256];
  121.   char szFile[256];
  122.   BOOL retcode;
  123.   char filters[MAXFILTERLEN];
  124.  
  125.   GenerateFileFilters (hParentWnd, filters);
  126.   while (fileName[0] == '\0' ||
  127.     (retcode = VerifyFileName (fileName)) == FAIL || 
  128.     (!appendOk && _access(AttachFileName, 0) == 0)) {
  129.     if (fileName[0] != '\0' && retcode == SUCCESS) {
  130.       /* file exists - ask if user wants to overwrite it */
  131.       sprintf (mybuf, "File %s exists.  OK to overwrite it?", fileName);
  132.       retcode = MessageBox (hParentWnd, mybuf, "File exists",
  133.           MB_YESNO | MB_DEFBUTTON2 | MB_ICONQUESTION);
  134.       if (retcode == IDYES)
  135.          break;
  136.     }
  137.  
  138. /*      Open dialog to ask user for a new file name */
  139.     memset (&ofn, 0, sizeof (OPENFILENAME));
  140.     if (fileName[0] != '\0')
  141.       sprintf (mybuf, "Invalid file name %s. Select new name", fileName);
  142.     else {
  143.        if (appendOk)
  144.           strcpy (mybuf, "Select new file name to create/append");
  145.        else
  146.           strcpy (mybuf, "Select new file name to create/overwrite");
  147.     }
  148.     ofn.lpstrTitle = mybuf;
  149.     strcpy (szDirName, startDir);
  150.     if (szDirName[0])
  151.         ofn.lpstrInitialDir = szDirName;
  152.     else
  153.         ofn.lpstrInitialDir = NULL;
  154.         
  155.     ofn.lStructSize = sizeof (OPENFILENAME);
  156.     ofn.hwndOwner = hParentWnd;
  157.     ofn.lpstrFilter = filters;
  158.     ofn.nFilterIndex = 1;
  159.     szFile[0] = '\0';
  160.     ofn.lpstrFile = szFile;
  161.     ofn.nMaxFile = sizeof (szFile);
  162.     ofn.lpstrFileTitle = NULL;
  163.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST;
  164.  
  165.     if (GetOpenFileName (&ofn) == 0)
  166.       return (FAIL);
  167.  
  168.     strcpy (fileName, strlwr (ofn.lpstrFile));
  169.   }
  170.   return (SUCCESS);
  171. }
  172. /* ------------------------------------------------------------------------
  173.  * Test file name is OK using SmartFiler settings
  174.  */
  175. BOOL
  176. VerifyFileName (char *fileName)
  177. {
  178.   char *beg, *ptr;
  179.  
  180.   if ((beg = strrchr (fileName, '\\')) == NULL)
  181.     beg = fileName;
  182.   else
  183.     beg++;        /* skip to after path slash */
  184.  
  185.   if ((ptr = strchr (beg, '.')) == NULL)
  186.     if (strlen (beg) <= MaxFileNameLen) /* no extension */
  187.       return (SUCCESS);
  188.  
  189.   if ((unsigned int)(ptr - beg) > MaxFileNameLen)
  190.     return (FAIL);
  191.  
  192. #ifndef WIN32
  193.   beg = ptr + 1;     /* multiple extensions (ok in NT)z */
  194.   if ((ptr = strchr (beg, '.')) != NULL)
  195.     return (FAIL);
  196. #endif
  197.  
  198.   if (strlen (beg) > MaxFileExtLen)    /* extension too long */
  199.     return (FAIL);
  200.  
  201.   return (SUCCESS);
  202. }
  203.  
  204. /* ------------------------------------------------------------------------
  205.  * SplitFileName into path, name, ext, fullName
  206.  * i.e. c:\windows\system\xyz.txt returns xyz.txt
  207.  * path     --> c:\windows\system  (note no final slash)
  208.  * name     --> xyz
  209.  * ext      --> txt
  210.  * fullName --> xyz.txt
  211.  *
  212.  * args must be pointers to buffers large enough to hold strings
  213.  * any of args can be NULL, in which case that fileName portion is ignored
  214.  */
  215. void
  216. SplitFileName(char *path, char *name, char *ext, char *fullName, char *fileName)
  217. {
  218.   char *start, *slash, *dot, *end;
  219.  
  220.   start = fileName;
  221.   end = start + strlen(fileName);
  222.  
  223.   if ((slash = strrchr (start, '\\')) == NULL) {
  224.      slash = start;                /* there is no path */
  225.   } else {
  226.      *slash = '\0';                /* replace last slash with null */
  227.      slash++;               
  228.   }
  229.   if (path) {
  230.      if (slash == start)
  231.         *path = '\0';        
  232.      else
  233.         strcpy (path, start);     /* from start to slash is the path */
  234.   }
  235.   
  236.   if (fullName)
  237.      strcpy (fullName, slash);
  238.   
  239.   if ((dot = strrchr (slash, '.')) == NULL) {
  240.      dot = end;
  241.   } else {
  242.      *dot = '\0';            /* replace last dot with null */
  243.      dot++;
  244.   }
  245.   if (name)
  246.      strcpy (name, slash);    /* from after the slash to dot is the name */
  247.  
  248.   if (ext) {
  249.      strcpy (ext, dot);     /* from after the dot to the end is the extension */
  250.   }
  251.   if (slash != start)
  252.      *(slash-1) = '\\';        /* restore the slash */
  253.   if (dot != end)
  254.      *(dot-1) = '.';        /* restore the dot */
  255. }
  256.  
  257. /* useful front ends to SplitFileName */
  258. char *GetFileExtension (char *ext, char *fileName)
  259. {
  260.   SplitFileName(NULL, NULL, ext, NULL, fileName);
  261.   return ext;
  262. }
  263.  
  264. char *NameWithoutPath (char *fullName, char *fileName)
  265. {
  266.   SplitFileName(NULL, NULL, NULL, fullName, fileName);
  267.   return fullName;
  268. }
  269.  
  270. char *PathWithoutName (char *path, char *fileName)
  271. {
  272.   SplitFileName(path, NULL, NULL, NULL, fileName);
  273.   return path;
  274. }
  275.  
  276.  
  277. /* ------------------------------------------------------------------------
  278.  * Smartfiler: HandleDupeName 
  279.  * check if dupe file name, and if there is, deal with it 
  280.  * return SUCCESS if dupe and handled (fixed name in newName),or if no dupe
  281.  * return FAIL on serious failure
  282.  */
  283. BOOL    
  284. HandleDupeName (HWND hParentWnd, char *fileName)
  285. {
  286.     char path[MAXFILENAME], name[MAXFILENAME], ext[MAXFILENAME], numStr[4];
  287.     char newName[MAXFILENAME], namecat[MAXFILENAME];
  288.     int i, len;
  289.     
  290.     if (_access(fileName, 0) < 0)
  291.         return SUCCESS;
  292.  
  293.     /* we have a dupe */
  294.     strcpy (newName, fileName);
  295.     SplitFileName(path, name, ext, NULL, fileName);
  296.     switch (OnDupeName)
  297.     {
  298.     case DUPE_AVOID_NONE:
  299.         return AskForNewFileName(hParentWnd, newName, path, FALSE);
  300.         break;
  301.         
  302.     case DUPE_AVOID_NUMBER_EXT:
  303.         for (i = 0; i < 999; i++) {
  304.             _snprintf(newName, MAXFILENAME, "%s\\%s.%03d", path, name, i);
  305.             if (_access(newName, 0) < 0)
  306.                 break;
  307.         }
  308.         break;
  309.     
  310.     case DUPE_AVOID_PREPEND_NUM:
  311.         for (i = 0; i < 999; i++) {
  312.             _snprintf(namecat, MAXFILENAME, "%d%s", i, name);
  313.             namecat[MaxFileNameLen] = '\0'; 
  314.             _snprintf(newName, MAXFILENAME, "%s\\%s.%s", path, namecat, ext);
  315.             if (_access(newName, 0) < 0)
  316.                 break;
  317.         }
  318.         break;
  319.                                 
  320.     case DUPE_AVOID_APPEND_NUM:
  321.         for (i = 0; i < 999; i++) {
  322.             itoa(i, numStr, 10);
  323.             len = MaxFileNameLen - strlen(numStr);
  324.             if (len <= 0) {
  325.                 i = 1000; break;
  326.             }
  327.             name[len] = '\0';
  328.             strcat(name, numStr);
  329.             name[MaxFileNameLen] = '\0'; 
  330.             _snprintf(newName, MAXFILENAME, "%s\\%s.%s", path, name, ext);
  331.             if (_access(newName, 0) < 0)
  332.                 break;
  333.         }
  334.         break;
  335.     }
  336.     if (i == 1000)    /* we failed to get an alternative, so ask user for help */
  337.         return AskForNewFileName(hParentWnd, fileName, path, FALSE);
  338.     else
  339.         strcpy (fileName, newName);
  340.  
  341.     return TRUE;    
  342. }
  343.  
  344. /* ------------------------------------------------------------------------
  345.  * SmartFiler: HandleLongName
  346.  * check if given name is longer than given maxLen, and if it is, deal with it 
  347.  * return SUCCESS if too long and handled (fixed name in newName), 
  348.  * or if name is not too long
  349.  * return FAIL if not handled
  350.  */
  351. BOOL
  352. HandleLongName(char *name, unsigned int maxLen)
  353. {
  354.     char newName[MAXFILENAME];
  355.     unsigned int skipped, len, src, dest;
  356.     BOOL result; 
  357.     
  358.     len = strlen(name);  
  359.     if (len <= maxLen)
  360.         return SUCCESS;
  361.     
  362.     /* name too long */
  363.     switch (OnNameTooLong)
  364.     {
  365.     case SHORTEN_NONE:
  366.         result = FAIL;
  367.         break;
  368.     
  369.     case SHORTEN_TRUNCATE:
  370.         name[maxLen] = '\0';
  371.         result = SUCCESS;
  372.         break;
  373.         
  374.     case SHORTEN_SKIP_VOWELS:
  375.         src = dest = skipped = 0;
  376.         while (dest < maxLen && src < len) {
  377.             if (len - skipped > maxLen && strchr("aeiou-_*!@@#$%^&*()+", name[src])) {
  378.                 skipped++;    
  379.             } else {
  380.                 newName[dest] = name[src];
  381.                 dest++;
  382.             }
  383.             src++;
  384.         }
  385.         newName[dest] = '\0';
  386.         strcpy (name, newName);
  387.         result = SUCCESS;
  388.         break;
  389.     }
  390.     return result;
  391. }
  392.  
  393. /* ------------------------------------------------------------------------
  394.  * SmartFiler: HandleExtensionCovnersion
  395.  * check if ext is in the extension mapping list, and if so,
  396.  * replace it with the mapped ext
  397.  */
  398. void
  399. HandleExtensionCovnersion(char *ext)
  400. {
  401.     register unsigned long i;
  402.     for (i = 0L; i < ExtMapSourceList->numLines; i++)
  403.     {
  404.         if (!_stricmp(TextBlockLine(ExtMapSourceList, i), ext)) {
  405.             strcpy(ext, TextBlockLine(ExtMapDosList, i));
  406.             break;
  407.         }
  408.     }
  409. }    
  410.  
  411. /* ------------------------------------------------------------------------
  412.  * SmartFiler
  413.  * processes fileName for dupe, too long, extension conversion
  414.  * returns SUCCESS if everything works, else FAIL
  415.  */
  416. BOOL
  417. SmartFile (HWND hParentWnd, char *fileName)
  418. {
  419.     char newName[MAXFILENAME];
  420.     char path[MAXFILENAME], name[MAXFILENAME], ext[MAXFILENAME];
  421.     BOOL result, result2;
  422.  
  423.     strcpy (newName, fileName);
  424.     SplitFileName(path, name, ext, NULL, fileName);
  425.  
  426.     if (VerifyFileName(fileName) == FAIL)
  427.     {
  428.         if (EnableExtensionConversion)
  429.             HandleExtensionCovnersion(ext);    
  430.  
  431.         result  = HandleLongName(name, MaxFileNameLen);
  432.         result2 = HandleLongName(ext, MaxFileExtLen);
  433.  
  434.         _snprintf(newName, MAXFILENAME, "%s\\%s.%s", path, name, ext);
  435.  
  436.         if (result == FAIL || result2 == FAIL) {
  437.             AskForNewFileName(hParentWnd, newName, path, FALSE);
  438.         }
  439.         
  440.     }
  441.     result = HandleDupeName(hParentWnd, newName);
  442.     strcpy(fileName, newName);
  443.     return result;
  444. }
  445.  
  446. @
  447.